<?php
function adxitCate($data,$cate=array()){    

  $sql="select * from `cate` where `pid`=".$data['pid'];//查找同级id

    $result=mysqli_query($GLOBALS['link'],$sql);

    while ( $arr=mysqli_fetch_assoc($result)) {
       $cate[$data['id']][]=$arr;

}
if ($data['pid']!=0) {

    $sql="select * from `cate` where `id`=".$data['pid']; //查询父级

    $result=mysqli_query($GLOBALS['link'],$sql);

    $data=mysqli_fetch_assoc($result);

    $cate=adxitCate($data,$cate);

    
  # code...
}
  return $cate=array_reverse($cate,true);

    }



      function upcate($pid=0,$cate=array(),$lv=0){  //传参
        $sql = "select * from `cate` where pid=$pid";//查询语句

       $result = mysqli_query($GLOBALS['link'], $sql);//运行



       while ($arr= mysqli_fetch_assoc($result)) {//取一条数据存入$arr

        $str='';  //声明一个空变量

        for($i=0;$i<$lv;$i++){
            $str.='|&nbsp';
        }
          $arr['name']=$str.$arr['name'];//拼接



        $cate[]=$arr;                                 

        $cate=upcate($arr['id'],$cate,$lv+1);//调用自己传参  没循环一次lv+1前面$i<$lv

          # code...
        } 

return $cate;   //返回值

  }
   